/*
 * Created by kopper on 31.01.15 18:51
 * (C) Copyright 2015 kopperek@gmail.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package app.android.kopper.foramemo;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import app.android.kopper.foramemo.util.AbstractTopProvider;
import app.android.kopper.foramemo.util.ForaMemoDbHelper;
import app.android.kopper.foramemo.util.LogProxy;

public class TopActivity extends Activity {

    public static final String KEY_TYPE="KEY_TYPE";

    public static final int TYPE_MOST_RECENT=0;
    public static final int TYPE_MOST_DIFFICULT=1;
    public static final int TYPE_MOST_FREQUENT=2;

    public static final int TYPE_REPETITION_SCHEDULE=3;

    public static AbstractTopProvider[] topProviders=new AbstractTopProvider[]{

            new AbstractTopProvider(
                    R.string.action_top20_most_recent,
                    "select "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_WORD+","+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ADDED+" from "+ForaMemoDbHelper.FeedEntry.TABLE_NAME+" where "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE+"=1 order by "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ADDED+" desc limit 20 "
            ) {
                @Override
                public String getCellContent(Cursor cursor,int column) {
                    switch(column) {
                        case 0:return (cursor.getString(0));
                        case 1:return (new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(cursor.getLong(1))));
                    }
                    return "?";
                }
            },

            new AbstractTopProvider(
                    R.string.action_top20_most_difficult,
                    "select "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_WORD+","+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_EP+" from "+ForaMemoDbHelper.FeedEntry.TABLE_NAME+"  where "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE+"=1  order by "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_EP+" asc limit 20 "
            ) {
                @Override
                public String getCellContent(Cursor cursor,int column) {
                    switch(column) {
                        case 0: return(cursor.getString(0));
                        case 1: return BigDecimal.valueOf(cursor.getDouble(1)).setScale(2,BigDecimal.ROUND_HALF_UP).toPlainString();
                    }
                    return "?";
                }
            },

            new AbstractTopProvider(
                    R.string.action_top20_most_frequent,
                    "select "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_WORD+","+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_REP_NO+" from "+ForaMemoDbHelper.FeedEntry.TABLE_NAME+"  where "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE+"=1  order by "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_REP_NO+" desc limit 20 "
            ) {
                @Override
                public String getCellContent(Cursor cursor,int column) {
                    switch(column) {
                        case 0: return(cursor.getString(0));
                        case 1: return String.valueOf((cursor.getInt(1)));
                    }
                    return "?";
                }
            },
            new AbstractTopProvider(
                    R.string.action_repetition_schedule,
                    "select "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+",count(1) " +
                            " from "+ForaMemoDbHelper.FeedEntry.TABLE_NAME+"  " +
                            " where "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE+"=1 and "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+" is not null  " +
                            " group by "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE +
                            " order by "+ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE+" asc " +
                            " limit 20 "
            ) {
                @Override
                public String getCellContent(Cursor cursor,int column) {
                    switch(column) {
                        case 0: return (new SimpleDateFormat("yyyy-MM-dd").format(new Date(cursor.getLong(0))));
                        case 1: return String.valueOf((cursor.getLong(1)));
                    }
                    return "?";
                }
            }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top);
        int type=getIntent().getExtras().getInt(KEY_TYPE);
        LogProxy.d("Type: "+type);
        try {
            AbstractTopProvider provider=topProviders[type];
            TextView titleText=(TextView)findViewById(R.id.title);
            titleText.setText(getText(provider.getTitle()));
            ColorStateList textColor=titleText.getTextColors();
            TableLayout table=(TableLayout)findViewById(R.id.table);
            createRowSpace(table);
            ForaMemoDbHelper dbHelper=new ForaMemoDbHelper(getApplicationContext());
            Cursor cursor=dbHelper.getReadableDatabase().rawQuery(provider.getSql(),null);
            while(cursor.moveToNext()) {
                TableRow tr=new TableRow(getApplicationContext());
                for(int a=0;a<cursor.getColumnCount();a++) {
                    TextView cell=new TextView(getApplicationContext());
                    cell.setText(provider.getCellContent(cursor,a));
                    cell.setTextColor(textColor);
                    cell.setPadding(10,2,10,2);
                    if(a>0)
                        cell.setGravity(Gravity.CENTER);
                    tr.addView(cell);
                }
                table.addView(tr);
                createRowSpace(table);
            }
            cursor.close();
        } catch(Exception e) {
            LogProxy.e("",e);
        }

    }

    private void createRowSpace(TableLayout table) {
        TableRow space=new TableRow(getApplicationContext());
        space.setBackgroundColor(Color.LTGRAY);
        space.setPadding(0, 0, 0, 1);
        table.addView(space);
    }
}
